package com.hzit.controller;


import com.hzit.entity.LoginUser;
import com.hzit.entity.User;
import com.hzit.service.IUserService;
import com.hzit.service.TokenService;
import com.hzit.utils.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.util.Map;
import java.util.Objects;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author author
 * @since 2024-08-31
 */
@RestController
@RequestMapping("/api/user")
@Api("用户登录")
public class UserController {

    @Autowired
    private IUserService service;

    @Autowired
    private TokenService tokenService;

    @PostMapping("login")
    @ApiOperation("账号密码登录")
    public R login(@RequestBody @ApiParam("登录用户") User user){
        LoginUser loginUser=service.login(user);
        if (Objects.isNull(loginUser)){
            return R.error().message("登录失败");
        }
        Map<String,Object> map=tokenService.getToken(loginUser);
        return R.ok().data(map).message("登录成功");
    }
}
